package com.log.enhance.util;

import org.apache.commons.lang3.RandomUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.UUID;

/**
 * @author Mr_wenpan@163.com
 * 日志公用工具类
 * @create: 2021/05/29 11:45
 */
public class LogCommonUtil {

    private static final String X_FORWARDED_FOR = "x-forwarded-for";
    private static final String PROXY_CLIENT_IP = "Proxy-Client-IP";
    private static final String WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
    private static final String UNKNOWN = "unknown";
    private static final String HTTP_CLIENT_IP = "HTTP_CLIENT_IP";
    private static final String HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR";

    /**
     * 生成日志的traceId
     *
     * @return java.lang.String
     * @date 2021/6/1 3:04 下午
     * @author wenpan
     */
    public static String generateTraceId() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取指定长度的随机字符串
     *
     * @param len 字符串长度
     * @return java.lang.String
     * @date 2021/6/1 9:46 下午
     * @author wenpan
     */
    public static String getRandomStr(int len) {
        char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
        char[] str = new char[len];
        for (int i = 0; i < len; i++) {
            str[i] = chars[RandomUtils.nextInt(0, 61)];
        }
        return new String(str);
    }

    /**
     * 获取请求的设备类型
     *
     * @param userAgent
     * @return java.lang.String
     * @date 2021/6/1 9:47 下午
     * @author wenpan
     */
    public static String getDevice(String userAgent) {
        String[] terminals = {
                "iphone", "android", "touch", "ipad", "symbian", "htc", "palmos", "blackberry", "opera mini", "windows ce", "nokia", "fennec", "macintosh",
                "hiptop", "kindle", "mot", "webos", "samsung", "sonyericsson", "wap", "avantgo", "eudoraweb", "minimo", "netfront", "teleca", "windows nt"
        };
        userAgent = userAgent.toLowerCase();
        for (String terminal : terminals) {
            if (userAgent.indexOf(terminal) != -1) {
                return terminal;
            }
        }
        return userAgent;

    }

    /**
     * 获取请求中IP地址
     *
     * @param request
     * @return java.lang.String
     * @date 2021/6/1 9:40 下午
     * @author wenpan
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader(X_FORWARDED_FOR);
        if (Objects.isNull(ip)) {
            ip = request.getHeader(PROXY_CLIENT_IP);
        }
        if (Objects.isNull(ip)) {
            ip = request.getHeader(WL_PROXY_CLIENT_IP);
        }
        if (Objects.isNull(ip)) {
            ip = request.getHeader(HTTP_CLIENT_IP);
        }
        if (Objects.isNull(ip)) {
            ip = request.getHeader(HTTP_X_FORWARDED_FOR);
        }
        if (Objects.isNull(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 测试：
     */
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(getRandomStr(10));
        }
    }
}
